﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer.DAL;
using DataAccessLayer;
using Common;
namespace BestBuy.ManagerViews
{
    public partial class Actions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.RoleID != (int)RoleType.Manager)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadAuctions();
            }
        }
      
        private void LoadAuctions()
        {
            gvWaitApproval.DataSource = AuctionDAL.GetAllWaittingAuction();
            gvWaitApproval.DataBind();
            gvAuctions.DataSource = AuctionDAL.GetAllOpenAuction();
            gvAuctions.DataBind();
            gvEndAuctions.DataSource = AuctionDAL.GetAllEndAuction();
            gvEndAuctions.DataBind();
            if (gvWaitApproval.Rows.Count == 0)
                lbNoItemApproval.Visible = true;
            if (gvAuctions.Rows.Count == 0)
                lbNoItemOpen.Visible = true;
            if (gvEndAuctions.Rows.Count == 0)
                lbNoItemEnd.Visible = true;
        }

        protected void btnApproval_Click(object sender, EventArgs e)
        {
            Button senderbutton = sender as Button;
            int auctionID = int.Parse(senderbutton.Attributes["ObjectId"]);
            AuctionDAL.ChangeStatusAuction(auctionID, (int)AuctionStatus.Active);
            LoadAuctions();
            MessagePanel.Show("Approval success.", UserControls.MessagePanel.MessageType.Success);
        }

        protected void btnBlock_Click(object sender, EventArgs e)
        {
            Button senderbutton = sender as Button;
            int auctionID = int.Parse(senderbutton.Attributes["ObjectId"]);
            AuctionDAL.ChangeStatusAuction(auctionID, (int)AuctionStatus.Inactive);
            LoadAuctions();
            MessagePanel.Show("Update success.", UserControls.MessagePanel.MessageType.Success);

        }
    }
}